/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package config.project;

import config.Block;
import java.io.File;
import java.util.Vector;

/**
 *
 * @author leng
 */
public class ProjectConf {

    /**the file name(not absolutely) of the configure file to each project*/
    protected final String projectfilename = "projectschema.cfg";

    /**Current project*/
    protected File __currentProject = null;


    /**class contracture*/
    public ProjectConf(File _project)
    {
        this.__currentProject = _project;
    }

    
    /**save configure of current project to disk*/
    public void saveConfigure()
    {
        // quit if curent project is unreachable
        if(__currentProject==null)
        {
            System.out.println("error 1102091124::fail to get the project");
            return;
        }

        // reach the config file to current project
        File config = ensureProjectConfFile(__currentProject);
        if(config==null)
        {
            System.out.println("error 1102091126A::fail to reach the config file to the project");
            return;
        }

        SaveConf saveconf = new SaveConf();
        saveconf.save(config);
    }

    

    /**Load project setting variables of eHOST from disk to memory.*/
    public void loadConfigure()
    {
        // quit if curent project is unreachable
        if(__currentProject==null)
        {
            System.out.println("error 1102091124::fail to get the project");
            return;
        }

        // reach the config file to current project
        File config = getProjectConfFile(__currentProject);
        if( config==null )
        {
            System.out.println("error 1102091126::fail to reach the config file to the project");
            return;
        }
        // split configure file into blocks
        config.Extracter extracter = new config.Extracter(config);
        Vector<Block> blocks = extracter.getBlocks();

        // remove all current markables
        ResultEditor.AnnotationClasses.Depot.removeAllElement();
        
        // load variable from block into memory       
        ParameterGather parameterloader = new ParameterGather();
        parameterloader.load( blocks );

    }
    

    private File ensureProjectConfFile(File _project){
        try{
            // ##1## this is ensure that this project folder exists
            if(!projectFolderExists(_project))
                return null;

            // ##2## this is to ensure there is a folder called "config" under
            // current project
            if(!configFolderExists(_project)){
                String projectpath = _project.getAbsolutePath();
                String conffolderpath = projectpath + File.separator + "config";
                File conffolder = new File(conffolderpath);
                conffolder.mkdirs();
            }

            
            // ##3## get the config name
            String projectpath2 = _project.getAbsolutePath();
            String configfolder = projectpath2 + File.separator + "config";
            if((configfolder==null)||(configfolder.trim().length()<1))
                return null;

            String configfile = configfolder + File.separator + this.projectfilename;

            File configure = new File(configfile);

            return configure;

            
        }catch(Exception ex){
            System.out.println("error 1102091140:: fail to ensure the exists " 
                    + "of configure file to current project."
                    + ex.toString() );
        }
        
        return null;
    }
    
    private File getProjectConfFile(File _project){
        
        try{

            // ##1## this is ensure that this project folder exists
            if(!projectFolderExists(_project))
                return null;

            // ##2## this is to ensure there is a folder called "config" under
            // current project
            if(!configFolderExists(_project))
                return null;

            // ##3## get the config name
            String configfolder = this.getConfigFolderName(_project);
            if((configfolder==null)||(configfolder.trim().length()<1))
                return null;

            String configfile = configfolder + File.separator + this.projectfilename;
            
            File configure = new File(configfile);

            if(configure.exists())
                return configure;
                        

        }catch(Exception ex){
            System.out.println("error 1102091041:: fail to get configure file to current project."
                    + ex.toString() );
        }
        return null;
    }

    private boolean projectFolderExists(File _project){
        try{
            // if the project is not exist
            if(_project==null)
                return false;

            if(!_project.exists())
                return false;

            //if this project is not a directory
            if(!_project.isDirectory())
                return false;

            return true;

        }catch(Exception ex){
            return false;
        }

    }

    

    private boolean configFolderExists(File _project){

        try{
            String projectpath = _project.getAbsolutePath();
            String conffolderpath = projectpath + File.separator + "config";
            File conffolder = new File(conffolderpath);

            return conffolder.exists();

        }catch(Exception ex)
        {
            return false;
        }
    }

    private String getConfigFolderName(File _project){
        String configpath = null;
        String projectpath = _project.getAbsolutePath();
        configpath = projectpath + File.separator + "config";
        
        return configpath;
    }

    
}
